﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Infancy.Core.Entities;
using Infancy.Core.Reflection;

namespace Infancy.Core.Service {
    public class QueryInfo<T> : IQueryInfo<T> where T : IEntity {
        public QueryInfo(T instanse) {
            Instanse = instanse;
            WhereQueries = new List<WhereQuery>();
            OrderQueries = new List<OrderQuery>();
        }

        #region IQueryInfo<T> Members

        public T Instanse {
            get;
            protected set;
        }

        public IList<WhereQuery> WhereQueries {
            get;
            protected set;
        }

        public IList<OrderQuery> OrderQueries {
            get;
            protected set;
        }

        public IQueryInfo<T> Where(Expression<Func<T, bool>> predicate) {
            BinaryExpression binaryExpression = (BinaryExpression)predicate.Body;
            WhereType wt;
            switch (binaryExpression.NodeType) {
                case ExpressionType.Equal:
                    wt = WhereType.Equal;
                    break;
                case ExpressionType.LessThan:
                    wt = WhereType.LessThan;
                    break;
                case ExpressionType.LessThanOrEqual:
                    wt = WhereType.LessThanOrEqual;
                    break;
                case ExpressionType.GreaterThan:
                    wt = WhereType.GreaterThan;
                    break;
                case ExpressionType.GreaterThanOrEqual:
                    wt = WhereType.GreaterThanOrEqual;
                    break;
                case ExpressionType.NotEqual:
                    wt = WhereType.NotEqual;
                    break;
                case ExpressionType.And:
                    wt = WhereType.And;
                    break;
                case ExpressionType.AndAlso:
                    wt = WhereType.And;
                    break;
                case ExpressionType.Or:
                    wt = WhereType.Or;
                    break;
                case ExpressionType.OrElse:
                    wt = WhereType.Or;
                    break;
                case ExpressionType.Not:
                    wt = WhereType.Not;
                    break;
                default:
                    throw new QueryTypeNotSupportException(binaryExpression.NodeType,
                        String.Format("ExpressionType:{0} not support for where query.", binaryExpression.NodeType));
            }

            return this;
        }

        public IQueryInfo<T> OrderBy(Expression<Func<T, object>> keySelector) {
            return this;
        }

        public IQueryInfo<T> OrderByDescending(Expression<Func<T, object>> keySelector) {
            return this;
        }

        #endregion
    }
}
